package com.bestcem.xm.ticket.service.impl;

import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.ticket.controller.param.ticket.notify.setting.UpdateSettingParam;
import com.bestcem.xm.ticket.dao.TicketNotifySettingDao;
import com.bestcem.xm.ticket.entity.mongo.NotifySetting;
import com.bestcem.xm.ticket.service.TicketNotifySettingService;
import com.bestcem.xm.ticket.service.convert.TicketNotifySettingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author liheng
 * @version v1.0
 * @date 2021/8/26 18:09
 */
@Slf4j
@Service
public class TicketNotifySettingServiceImpl implements TicketNotifySettingService {

    @Resource
    private TicketNotifySettingDao ticketNotifySettingDao;

    @Resource
    private TicketNotifySettingMapper ticketNotifySettingMapper;

    @Override
    public ServiceResult<NotifySetting> getEntityByOrgId(String orgId) {
        NotifySetting notifySetting = ticketNotifySettingDao.findByOrgId(orgId);
        if (Objects.isNull(notifySetting)) {
            notifySetting = new NotifySetting();
            notifySetting.setOrgId(orgId);
            NotifySetting.Way way = new NotifySetting.Way();
            way.setEmail(true);
            notifySetting.setWays(way);
        }
        return ServiceResult.success(notifySetting);
    }


    @Override
    public ServiceResult<String> updateSetting(UpdateSettingParam param) {
        NotifySetting notifySetting = new NotifySetting();
        notifySetting.setWays(ticketNotifySettingMapper.wayToEntiyWay(param.getWay()));
        notifySetting.setOrgId(param.getOrgId());

        NotifySetting entity = ticketNotifySettingDao.upsertSetting(param.getOrgId(), notifySetting);

        return ServiceResult.success(entity.getId());
    }

}
